package com.cn.serverline.lineicp.workshopPlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.roses.kernel.office.api.pojo.report.ExcelExportParam;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.workshopPlan.entity.LineAssembleBatchSplit;
import com.cn.serverline.lineicp.workshopPlan.entity.WeekStageDaysTasks;
import com.cn.serverline.lineicp.workshopPlan.entity.request.LineAssembleBatchSplitRequest;
import com.cn.serverline.lineicp.workshopPlan.entity.request.LineAutomaticTasksRequest;
import com.cn.serverline.lineicp.workshopPlan.enums.Constant;
import com.cn.serverline.lineicp.workshopPlan.enums.LinePlanEnum;
import com.cn.serverline.lineicp.workshopPlan.enums.RolingPlanException;
import com.cn.serverline.lineicp.workshopPlan.mapper.LineAssembleBatchSplitMapper;
import com.cn.serverline.lineicp.workshopPlan.service.LineAssembleBatchSplitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 周总装批次拆分服务实现类
 */
@Service("lineAssembleBatchSplitService")
public class LineAssembleBatchSplitServiceImpl extends ServiceImpl<LineAssembleBatchSplitMapper, LineAssembleBatchSplit> implements LineAssembleBatchSplitService {


    @Resource(name = "lineAssembleBatchSplitService")
    private LineAssembleBatchSplitServiceImpl weekStageDaysTasksService;

    /**
     * 查询周总装批次拆分列表
     * @param lineAssembleBatchSplitRequest 请求参数
     * @return
     */
    @Override
    public List<LineAssembleBatchSplitRequest> findList(LineAssembleBatchSplitRequest lineAssembleBatchSplitRequest) {
        if (lineAssembleBatchSplitRequest.getStartDate() != null && lineAssembleBatchSplitRequest.getEndDate() != null) {
            String startTime = DateUtil.format(lineAssembleBatchSplitRequest.getStartDate(), "yyyy-MM-dd");
            String endTime = DateUtil.format(lineAssembleBatchSplitRequest.getEndDate(), "yyyy-MM-dd");
            lineAssembleBatchSplitRequest.setStartDateString(startTime);
            lineAssembleBatchSplitRequest.setEndDateString(endTime);    

        }
        return baseMapper.findList(lineAssembleBatchSplitRequest);
    }

    /**
     * 撤回拆分批次
     * @param planWeekStageDaysTasks 列表参数
     * @return 是否成功
     */
    @Override
    public Boolean splitBatch(List<WeekStageDaysTasks> planWeekStageDaysTasks) {
        return true;
    }

    /**
     * 查询周部装组件拆分表排产状态
     * @param weekStageDaysTasksIds 批次id数组
     * @return 是否存在已排产
     */
    private boolean isProduceScheduledStatus(List<Long> weekStageDaysTasksIds) {
        //查询周部装组件拆分表排产状态
        List<LineAssembleBatchSplit> lineAssembleSplits = this.list(new LambdaQueryWrapper<LineAssembleBatchSplit>()
                .in(LineAssembleBatchSplit::getPlanWeekStageDaysTasksId, weekStageDaysTasksIds));
        //已排产
        return lineAssembleSplits.stream().map(LineAssembleBatchSplit::getBuildStatus)
                .collect(Collectors.toList()).contains(LinePlanEnum.PRODUCTION_SCHEDULED.getState());
    }


}
